package org.example.quartz.BIP;

import cn.hutool.db.Entity;
import cn.hutool.db.Session;
import cn.hutool.extra.spring.SpringUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.example.quartz.service.LogService;
import org.example.quartz.service.OrgService;
import org.example.quartz.task.QuartzTaskJob;
import org.example.quartz.util.DataSourceUtil;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
// 再Job被执行结束后，将会更新JobDataMap，这样下次Job执行后悔使用新的值而不是初始值
@PersistJobDataAfterExecution
// 同一时间将只有一个Job实例被执行，为了避免并发问题导致数据紊乱，建议这两个注解一起使用
@DisallowConcurrentExecution
public class OrgQuartz_BIP implements QuartzTaskJob {


    //日志
    LogService loginfo = SpringUtil.getBean(LogService.class);
    //orgService
    OrgService orgService = SpringUtil.getBean(OrgService.class);

    /**
     * YY组织架构同步
     **/
    @SneakyThrows
    @Override
    public void execute(JobExecutionContext context) {

        /**
         *
         * */
        String methodName = "OrgQuartz";
        //JdbcTemplate jdbcTemplate = new JdbcTemplate();
        JdbcTemplate jdbcTemplate = SpringUtil.getBean(JdbcTemplate.class);

        //2、根据时间查询数据集 (停用和删除的过滤掉，暂不考虑，线下处理)全量
        String selectSQL = "SELECT \n" +
                "O.CODE AS ORGCODE,\n" +
                "O.NAME AS ORGNAME,\n" +
                "O.TS AS TS\n" +
                "FROM \n" +
                "ORG_ORGS O \n" +
                "WHERE \n" +
                "DR =0 \n" +
                "AND\n" +
                "ENABLESTATE = 2";
        //查询数据，根据数据结果推送给GS浪潮
        Session oracleSession = DataSourceUtil.getOracleSession();
        List<Entity> query = oracleSession.query(selectSQL);
        //拼装数据，推送给中间库
        if (query.size() > 0) {
            for (Map<String, Object> gSmap : query) {
                int x = orgService.insertOrg((String) gSmap.get("orgName"), (String) gSmap.get("orgCode"));
                //成功
                if (x > 0) {
                    int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "成功", (String) gSmap.get("orgName") + (String) gSmap.get("orgCode"), "成功", "", "", "", (String) gSmap.get("TS"));
                    if (loginsert > 0) {
                        //System.out.println("日志写入成功！！");
                    } else {
                        System.out.println("日志写入失败！！");
                    }
                    //失败
                } else {
                    //写入日志表
                    int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "失败", (String) gSmap.get("orgName") + (String) gSmap.get("orgCode"), "成功", "", "", "", (String) gSmap.get("TS"));
                    if (loginsert > 0) {
                        //System.out.println("日志写入成功！！");
                    } else {
                        System.out.println("日志写入失败！！");
                    }
                }
            }
        } else {
            System.out.println(methodName + ":暂无新数据推送");
        }
    }


}
